<?php

/**
 * 导入类
 */
use EasyWeChat\Factory;

/**
 * 微信支付统一下单类
 */
class WechatPayUnify {
    protected $app = null;
    
    function __construct(){
        $config = [
            "app_id" => '',
            "mch_id" => '',
            "key" => '',
            "cert_path" => '',
            "notify_url" => ''
        ];
        $this->app = Factory::payment($config);
    }

    public function unify($obj){

        if(!isset($obj['orderid']) || empty($obj['orderid'])) return false;
        if(!isset($obj['amount']) || empty($obj['amount'])) return false;
        if(!isset($obj['notify_url']) || empty($obj['notify_url'])) return false;
        if(!isset($obj['openid']) || empty($obj['openid'])) return false;
        
        $result = $app->order->unify([
            "body" => $obj['body'],
            "out_trade_no" => $obj['orderid'],
            "total_fee" => $obj['amount'],
            "spbill_create_id" => $obj['ip'],
            "notify_url" => $obj['notify_url'],
            "trade_type" => 'JSAPI',
            "openid" => $obj['openid']
        ]);

        if($result['result_code'] == "SUCCESS"){
            return $result;
        }else{
            return false;
        }
    } 
}
